package com.woniuxy.controller;

import com.woniuxy.controller.util.ExcelReader;
import com.woniuxy.entity.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @Author: 曾令忠
 * @Description:
 * @Date: 2020/8/7 0007-下午 2:47
 */
@RestController
public class UplocadController {

    @PostMapping("/upload")
    public ResponseEntity upload(@RequestParam("userfile")MultipartFile userfile){
        // 检查前台数据合法性
        if (null == userfile || userfile.isEmpty()) {
            System.out.println("上传的Excel商品数据文件为空！上传时间：" + new Date());
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        try {
            // 解析Excel
            List<Info> parsedResult = ExcelReader.readExcel(userfile);
            // todo 进行业务操作
            System.out.println(parsedResult);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            System.out.println("上传的Excel商品数据文件为空！上传时间：" + new Date());
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }
}
